package com.zhiyou100.HibernateDemo01_3;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate5.HibernateTemplate;

import com.zhiyou100.dao.TeacherDAO;
import com.zhiyou100.model.Teacher;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        m4();
    }
  //删除
  	public static void m6() {
  		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
  		TeacherDAO teacherDAO = context.getBean(TeacherDAO.class);
  		teacherDAO.deleteById(4);
  	}
  	//查询单个+修改
  	public static void m5() {
  		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
  		TeacherDAO teacherDAO = context.getBean(TeacherDAO.class);
  		Teacher teacher = teacherDAO.queryById(2);
  		System.out.println(teacher);
  		teacher.setName("898797");;
  		teacherDAO.update(teacher);
  		
  	}
  	
  	//一定要开启事务
  	//dao的添加数据
  	public static void m4() {
  		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
  		TeacherDAO teacherDAO = context.getBean(TeacherDAO.class);
  		Teacher teacher = new Teacher();
  		teacher.setName("quwquququ");
  		teacherDAO.add(teacher);
  	}
    //dao的查询全部
    public static void m3() {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
		TeacherDAO teacherDAO = context.getBean(TeacherDAO.class);
		List<Teacher> teachers = teacherDAO.queryAll();
		System.out.println(teachers);
		
		
	}
    
    
    /****************传统写法，现在采用dao设计模式******************/
    /*	//添加
     * //我们在使用hibernate中必须开始事务,如果不开启事务，是不能进行修改数据库的DML，只能进行查询操作DQL
    	
    	public static void m3() {
    		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    		HibernateTemplate hibernateTemplate = context.getBean(HibernateTemplate.class);
    		User user  = new User();
    		user.setAccount("qwer");
    		user.setPassword("ahahahahaah");
    		hibernateTemplate.save(user);
    		
    		
    	}
    */	//查询单个
    	public static void m2() {
    		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    		HibernateTemplate hibernateTemplate = context.getBean(HibernateTemplate.class);
    		Teacher user = hibernateTemplate.get(Teacher.class, 1);
    		System.out.println(user);
    	}
    	//查询
    	public static void m1() {
    		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    		HibernateTemplate hibernateTemplate = context.getBean(HibernateTemplate.class);
    		List<Teacher> users = hibernateTemplate.loadAll(Teacher.class);
    		System.out.println(users);
    		
    	}
    
}
